package com.zy.hystrix;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MyController {

    //注入远程调用对象
    @Autowired
    RestTemplate rt;


    @RequestMapping("/hystrix")
    @HystrixCommand(fallbackMethod="backMethod")
    public  String getMesg(){
    //rpc远程调用出现异常---路径错，---代码错----》断路器容错
        String forObject = rt.getForObject("http://eureka-client/client1", String.class);
        System.out.println(forObject);

        return forObject;
    }


    //准备一个回调函数
    public String backMethod(){
        System.out.println("断路器调用回调函数，业务逻辑自己规定");
        return "您的rpc调用失败，我是断路器的回调函数。";
    }

}
